package com.protocolmeter.STS;

import com.protocolmeter.function.FrameFunction;

public class Get_Token {

	/**
	 * @Title: Get_Token.java
	 * @Description: 根据数据标示，获取不同组的token
	 * @param identification
	 * @return
	 */
	public String GetToken(String control, String identification, String data, String keys) {

		STAFunction function = new STAFunction();

		FrameFunction frameFunction = new FrameFunction();
		String keys1 = frameFunction.Reverse(keys);
		byte[] key = new byte[8];
		for (int i = 0; i < 8; i++) {
			int keyvalue = Integer.valueOf(keys1.substring(i * 2, i * 2 + 2), 16);
			key[i] = (byte) keyvalue;
		}

		String token = "";
		switch (control) {
		case "03":
			switch (identification) {
			case "070301FF":// 密钥更新
			{
				// 密钥1，密钥2
				token = token(function, key, data.substring(0, 8)) + token(function, key, data.substring(8, 16));

			}
				break;
			case "070103FF":// 钱包初始化
			{
				// 金额，次数
				String[] datas = data.split("-");
				String money = Integer.toHexString(Integer.valueOf(datas[0]));
				String num = Integer.toHexString(Integer.valueOf(datas[1]));
				token = token(function, key, num) + token(function, key, money);

			}

				break;
			case "070101FF":// 开户
			{
				// 金额，次数，客户编号
				String[] datas = data.split("-");
				String money = Integer.toHexString(Integer.valueOf(datas[0]));
				String num = Integer.toHexString(Integer.valueOf(datas[1]));
				token = token(function, key, datas[2]) + token(function, key, num) + token(function, key, money);

			}

				break;
			case "070102FF":// 充值
			{
				// 金额，次数，客户编号
				String[] datas = data.split("-");
				String money = Integer.toHexString(Integer.valueOf(datas[0]));
				String num = Integer.toHexString(Integer.valueOf(datas[1]));
				token = token(function, key, datas[2]) + token(function, key, num) + token(function, key, money);

			}

				break;
			default:
				break;
			}
			break;
		case "14":
			switch (identification) {
			case "04001006":// 退费
			{
				// 金额
				String[] datas = data.split("-");
				String money = Integer.toHexString(Integer.valueOf(datas[0]));
				token = token(function, key, money);

			}

				break;
			case "04000108":// 费率切换时间
			case "04000109":// 2套阶梯时间
			case "04000402":// 设置表号
			case "04001002": // 报警金额
			{
				token = token(function, key, data);
			}
				break;
			case "040501FF":// 当前费率电价
			case "040502FF":// 备用费率电价
			{
				// 金额
				String[] datas = data.split("-");
				token = token(function, key, datas[3]) + token(function, key, datas[2]) + token(function, key, datas[1])
						+ token(function, key, datas[0]);

			}
				break;
			case "04060500":// 备用阶梯值6
			{
				// 金额
				String[] datas = data.split("-");
				token = token(function, key, datas[5]) + token(function, key, datas[4]) + token(function, key, datas[3])
						+ token(function, key, datas[2]) + token(function, key, datas[1])
						+ token(function, key, datas[0]);

			}
				break;
			case "04060501":// 备用阶梯电价7
			{
				// 金额
				String[] datas = data.split("-");
				token = token(function, key, datas[6]) + token(function, key, datas[5]) + token(function, key, datas[4])
						+ token(function, key, datas[3]) + token(function, key, datas[2])
						+ token(function, key, datas[1]) + token(function, key, datas[0]);

			}
				break;
			case "04060502":// 年结算2
			{
				// 金额
				String[] datas = data.split("-");
				token = token(function, key, datas[1]) + token(function, key, datas[0]);

			}
				break;
			default:
				break;
			}
			break;
		case "1C":// 拉合闸
		{
			// 控制命令-有效时间
			String[] datas = data.split("-");
			token = token(function, key, datas[1]) + token(function, key, frameFunction.Reverse(datas[0]));

		}
			break;
		default:
			break;
		}

		return token;

	}

	private String token(STAFunction function, byte[] key, String data) {
		String dataSTS = data;
		while (dataSTS.length() < 14) {
			dataSTS = "0" + dataSTS;
		}
		String token = function.getToken(dataSTS, key, true);
		System.out.println("明文" + dataSTS + "密文" + token + "keys" + key);
		return token;
	}

}
